import 'dart:io';

import 'package:dio/dio.dart';
import 'package:time/test/test_file.dart';

class DioHttp {
  var savePath = "";
  var fileName = "temp.apk";

  /**
   * 下载
   */
  Future<CancelToken> requestDown(String downloadUrl, ProgressCallback? progressCallback) async {
    var path = await TestFile().getPath();
    savePath = path + "/" + fileName;
    var cancelToken = CancelToken();
    Dio().download(downloadUrl, savePath, onReceiveProgress: progressCallback, cancelToken: cancelToken);
    return cancelToken;
  }

  /**
   * 上传
   */
  upload(CancelToken? cancelToken, ProgressCallback? progressCallback) async {
    if (!await File(savePath).exists()) {
      return;
    }
    print(savePath + "  " + (await File(savePath).exists()).toString());
    var upload = "http://waterfairy.top/account/file/uploadFile";

    Map<String, dynamic> map = {};
    map["fileType"] = "other";
    map["project"] = "files";
    map["file"] = await MultipartFile.fromFile(
      savePath,
      filename: fileName,
    );
    var formData = FormData.fromMap(map);
    var response = await Dio().post(upload, data: formData, onSendProgress: progressCallback, cancelToken: cancelToken);
    print(response.data.toString());
  }
}
